package com.lut.edu.apm.webCenter;

import com.lut.edu.apm.commodityCenter.pojo.CommodityLibrary;
import com.lut.edu.apm.commodityCenter.service.CommodityLibraryService;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 商品库初始化
 *
 * @author Zhao Suzhen
 * @date `2024/4/13`
 */
@SpringBootTest
public class CommodityLibraryInit {
    private Logger logger = LoggerFactory.getLogger(CommodityLibraryInit.class);

    @Autowired
    CommodityLibraryService commodityLibraryService;

    @Test
    public void init() {
        System.out.println("商品库初始化");
//        readFile();
    }
//
//    public void readFile() {
//        Resource resource = new ClassPathResource("19年中国商品百万条形码数据.txt");
//        Pattern pattern = Pattern.compile("\"([^\"]*)\"");
//        Set<String> bathInfo = new HashSet<>();
//        List<CommodityLibrary> saveBathInfo = new ArrayList<>();
//
//        try (InputStream inputStream = resource.getInputStream();
//             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
//            String line;
//            while ((line = reader.readLine()) != null) {
//                Matcher matcher = pattern.matcher(line);
//                List<String> items = new ArrayList<>();
//                while (matcher.find()) {
//                    items.add(matcher.group(1));
//                }
//                if (items.size() >= 6) {
//                    CommodityLibrary commodityLibrary1 = commodityLibraryService.queryById(items.get(0));
//                    if (null == commodityLibrary1 && !bathInfo.contains(items.get(0))) {
//                        CommodityLibrary commodityLibrary = new CommodityLibrary();
//                        commodityLibrary.setItemNo(items.get(0));
//                        commodityLibrary.setItemName(items.get(1));
//                        commodityLibrary.setPym(items.get(2));
//                        commodityLibrary.setItemSize(items.get(3));
//                        commodityLibrary.setUnitNo(items.get(4));
//                        commodityLibrary.setProductArea(items.get(5));
//                        bathInfo.add(items.get(0));
//                        saveBathInfo.add(commodityLibrary);
//                    }
//                }
//                if (saveBathInfo.size() >= 200) {
//                    boolean status = commodityLibraryService.saveBatch(saveBathInfo);
//                    if (status) {
//                        logger.info("成功加入数据库：" + saveBathInfo.toString());
//                        saveBathInfo.clear();
//                    }
//                }
//            }
//            if (!saveBathInfo.isEmpty()) {
//                boolean status = commodityLibraryService.saveBatch(saveBathInfo);
//                if (status) {
//                    logger.info("最后加入数据库：" + saveBathInfo.toString());
//                }
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
}
